package com.qust.EnDecryption.starter;

import com.qust.EnDecryption.common.Constant;
import com.qust.EnDecryption.handle.AlgorithmTransmissionHandle;
import com.qust.EnDecryption.handle.AlgorithmicResourceCatchUpHandle;
import com.qust.EnDecryption.handle.EnDecryptionHandle;
import com.qust.EnDecryption.listener.ConnectListener;
import com.qust.EnDecryption.zookeeper.ZkNode;
import com.qust.EnDecryption.zookeeper.ZkNodeProvide;
import com.qust.agreement.EnDecAgreement;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import lombok.extern.log4j.Log4j2;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
@Log4j2
public class Starter {
    public Starter(){
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup work = new NioEventLoopGroup();
        AlgorithmTransmissionHandle algorithmTransmissionHandle = new AlgorithmTransmissionHandle(Constant.NAME_IEnDecryption_MAP);
        AlgorithmicResourceCatchUpHandle algorithmicResourceCatchUpHandle = new AlgorithmicResourceCatchUpHandle();
        log.info("连接zookeeper");
        CuratorFramework client = ZkNodeProvide.connectZk();
        ZkNodeProvide zkNodeProvide = new ZkNodeProvide(client, algorithmTransmissionHandle,algorithmicResourceCatchUpHandle);
        ZkNode zkNode = new ZkNode(Constant.ip + ":" + Constant.port, Runtime.getRuntime().availableProcessors());
        zkNodeProvide.register(zkNode);
        zkNodeProvide.setWatch(work, Constant.MESSAGE_HANDLE);

        // 分布式锁
        InterProcessMutex interProcessMutex = new InterProcessMutex(client,"/lock");
        algorithmTransmissionHandle.setInterProcessMutex(interProcessMutex);

        log.info("绑定bind");
        ChannelFuture bind = serverBootstrap
                .group(boss, work)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioServerSocketChannel) throws Exception {
                        nioServerSocketChannel.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024 * 1024 * 1024, 16, 4, 0, 0));
                        // 协议解密
                        nioServerSocketChannel.pipeline().addLast(new EnDecAgreement());
                        nioServerSocketChannel.pipeline().addLast(new EnDecryptionHandle(Constant.NAME_IEnDecryption_MAP));
                        nioServerSocketChannel.pipeline().addLast(algorithmicResourceCatchUpHandle);
                        nioServerSocketChannel.pipeline().addLast(algorithmTransmissionHandle);

//                        nioServerSocketChannel.pipeline().addLast(new ExceptionHandler());
                    }
                }).bind(Constant.port).addListener(new ConnectListener(interProcessMutex,work));

        // 开启监控线程
        log.info("开启监控线程");
        new Thread(new com.qust.EnDecryption.monitor.Starter(Constant.monitoringPort)).start();
    }
}
